package nl.txproductions.twitterclient.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;

import nl.txproductions.twitterclient.model.Message;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventListModel;

public class MessageJList extends JList {

	public MessageJList(EventList<Message> messageEventList) {
		super(new EventListModel<Message>(messageEventList));
		this.init();
	}

	private void init() {
		this.setSize(300, (int) this.getSize().getHeight());
		this.setCellRenderer(new MessageListCellRenderer());
		this.setBackground(new Color(247, 253, 255, 255));
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
	}

	private class MessageListCellRenderer implements ListCellRenderer {

		@Override
		public Component getListCellRendererComponent(JList list, Object value,
				int index, boolean isSelected, boolean cellHasFocus) {
			Message message = (Message) value;

			JPanel panel = new JPanel();
			GroupLayout layout = new GroupLayout(panel);
			panel.setLayout(layout);
			panel.setOpaque(false);
			panel.setBorder(BorderFactory.createMatteBorder(0, 0, 2, 0,
					new Color(86, 192, 227)));

			JLabel usernameLabel = new JLabel(message.getUser().getScreenName());
			Font font = new Font("Calibri", Font.BOLD, 14);
			usernameLabel.setFont(font);

			JLabel fullnameLabel = new JLabel(" " + message.getUser().getName());
			font = new Font("Calibri", Font.PLAIN, 12);
			fullnameLabel.setFont(font);
			fullnameLabel.setForeground(Color.GRAY);

			JTextArea messageTextArea = new JTextArea();
			messageTextArea.setLineWrap(true);
			messageTextArea.setWrapStyleWord(true);

			messageTextArea.setText(message.getText());

			messageTextArea.setEditable(false);
			messageTextArea.setOpaque(false);

			layout.setHorizontalGroup(layout
					.createParallelGroup()
					.addGroup(
							layout.createSequentialGroup()
									.addComponent(usernameLabel)
									.addComponent(fullnameLabel))
					.addGroup(
							layout.createSequentialGroup().addComponent(
									messageTextArea)));
			layout.setVerticalGroup(layout
					.createSequentialGroup()
					.addGroup(
							layout.createParallelGroup(
									GroupLayout.Alignment.LEADING)
									.addComponent(usernameLabel)
									.addComponent(fullnameLabel))
					.addGroup(
							layout.createSequentialGroup().addComponent(
									messageTextArea)));

			return panel;
		}
	}
}